using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using RSAExtensions;

namespace MyEncrytExensions
{
    public static class StringEncryptExtensions
    {
    private static readonly string PublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+MSHAGuSiM0p0jBo+V/j81Tx2YKzH9iXwlDvXNWuMMsmeIMmVjVEIDGq+jexox4pMEzjsDJ9ecLMl9lRiX+7HSvNGK4cocy5nrhtyhP74x418ZZ4kzbQPGpJE/mGdNkSSTvJ4idxXJtaNAgLfBFqYfbuuuZpgTeXuyTLdepkBoQIDAQAB";
    //数据库连接字符串私钥
    private static readonly string PrivateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL4xIcAa5KIzSnSMGj5X+PzVPHZgrMf2JfCUO9c1a4wyyZ4gyZWNUQgMar6N7GjHikwTOOwMn15wsyX2VGJf7sdK80YrhyhzLmeuG3KE/vjHjXxlniTNtA8akkT+YZ02RJJO8niJ3Fcm1o0CAt8EWph9u665mmBN5e7JMt16mQGhAgMBAAECgYBBA8SYydisRcJG8vmppCL+Mtp3UPbfaBpyEgoeuCkqhNX9ExI16Ffx22TF/Pf6nXN+K/gCYRI9k2L6MCmKc2bI7MjqvWyXnPz5GVJotc8gnvHc2sLrA9VuaMwwQDrwl3MWoBaWOHDx/DP6j8zW2F/32f5rku+NKTm4sO3G726ljQJBANi9jiSjHqI+GUgi1Sm1KaMYWlGeXxQ+nlK3z7aVQQUGLtdscLu+xfmYFDn+hT+PwBANI/Z6vlGb47V1U1yZupcCQQDgpH9+UomTRY0Mps7g/3uWScf92ymlNHavk1MBh/6CT5AB6f2fkj5lxiXS0ohmaDo7oNWjuHrQ77aljjXUNMSHAkEAk/tIM8eTwN55UIFH3UQt+4dDE3r4G9BtNLj/fwAE9rM0BC2sAuPMNBAbr0USIUW6srrmFQHVM88l99z1X8IuXQJBAI5IAixbUKHDqou510xw5LESuCXUOzNU4FtiP2+31/5nMDnWH3qDIRxK/UfjatsZ5tA/lTDmNnvBaHjBctyeM18CQFJqPaZvdoQr9Yedoe5eg1DCc/tAaDwRxlruASlJO6uHh75yWPQOesSvdTqeKkzZK5b2bpymePYzycm/vCMnHpg=";
        public static RSA GetNewRsa()
        {
            // Add services to the container.
            var rsa = RSA.Create(1024);//创建加密
            rsa.ImportPublicKey(RSAKeyType.Pkcs8, PublicKey); //公钥
            rsa.ImportPrivateKey(RSAKeyType.Pkcs8, PrivateKey); // 私钥
            return rsa;
        }

        public static RSA GetNewRsa(string publicKey,string privateKey,int  Keysize)
        {
            // Add services to the container.
            var rsa = RSA.Create(Keysize);//创建加密
            rsa.ImportPublicKey(RSAKeyType.Pkcs8, publicKey); //公钥
            rsa.ImportPrivateKey(RSAKeyType.Pkcs8, privateKey); // 私钥
            return rsa;
        }
       
        public static string DecryptText(this string text) => GetNewRsa().DecryptBigData(text, RSAEncryptionPadding.OaepSHA1);

        public static string DecryptText(this string text,RSAEncryptionPadding rSAEncryptionPadding,string publicKey,string privateKey,int  Keysize) => GetNewRsa(publicKey,privateKey,Keysize).DecryptBigData(text, rSAEncryptionPadding);


        public static string EncryptText(this string text) => GetNewRsa().EncryptBigData(text, RSAEncryptionPadding.OaepSHA1);
        public static string EncryptText(this string text,RSAEncryptionPadding rSAEncryptionPadding,string publicKey,string privateKey,int  Keysize) => GetNewRsa(publicKey,privateKey,Keysize).EncryptBigData(text, rSAEncryptionPadding);
    }
}